﻿Imports NAudio.Wave
Imports System.IO
Imports NAudio.Utils

Namespace Audio
	''' <summary>
	''' Extends the NAudio AiffFileReader class to allow for looping of the audio.
	''' </summary>
	''' <remarks></remarks>
	Public Class LoopingAiffFileReader
		Inherits NAudio.Wave.AiffFileReader

		''' <summary>
		''' implements one of the aifffilereader's new subroutines.
		''' </summary>
		''' <param name="file"></param>
		''' <remarks></remarks>
		Public Sub New(file As String)
			MyBase.new(file)
		End Sub

		''' <summary>
		''' implements one of the aifffilereader's new subroutines.
		''' </summary>
		''' <param name="inputstream"></param>
		''' <remarks></remarks>
		Public Sub New(inputstream As System.IO.Stream)
			MyBase.new(inputstream)
		End Sub

		''' <summary>
		''' When set to true the reader will loop through the audio file repeatedly.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property EnableLooping As Boolean
			Get
				Return EnableLoopingProperty
			End Get
			Set(value As Boolean)
				EnableLoopingProperty = value
			End Set
		End Property

		''' <summary>
		''' Represents the EnableLooping Property.
		''' </summary>
		''' <remarks></remarks>
		Private EnableLoopingProperty As Boolean = False


		''' <summary>
		''' Sets the point that the reader will start at.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property StartTime As TimeSpan
			Get
				Return StartTimeProperty
			End Get
			Set(value As TimeSpan)
				If StartTimeProperty <> value Then
					StartTimeProperty = value
					MyBase.CurrentTime = StartTime
				End If
			End Set
		End Property

		Private StartTimeProperty As New TimeSpan(0)

		''' <summary>
		''' Overrides the AiffFileReader's Read function to allow for looping the audio cue if EnableLooping is set to true.
		''' </summary>
		''' <param name="buffer"></param>
		''' <param name="offset"></param>
		''' <param name="count"></param>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Overrides Function Read(buffer() As Byte, offset As Integer, count As Integer) As Integer
			Dim totalBytesRead As Integer = 0
			While totalBytesRead < count
				Try
					Dim bytesRead As Integer = MyBase.Read(buffer, offset + totalBytesRead, count - totalBytesRead)
					If MyBase.TotalTime.TotalMilliseconds - MyBase.CurrentTime.TotalMilliseconds <= 0 Then
						If MyBase.Position = 0 OrElse Not EnableLooping Then
							Exit While
						End If
						'MyBase.Seek(0, SeekOrigin.Begin) 'Replaced 2/22/15
						MyBase.CurrentTime = StartTime
						totalBytesRead += bytesRead
						Return 0
					End If
					totalBytesRead += bytesRead
				Catch
					Return 0
				End Try
			End While
			RaiseEvent DoRead(Me, New EventArgs)
			Return totalBytesRead
		End Function


		Public Event DoRead(sender As Object, e As EventArgs)

	End Class
End Namespace